<template>
  <div :class="className" :style="{ height: height, width: width }" />
</template>

<script>
import * as echarts from "echarts";
import resize from "./mixins/resize";
function fontSize(font) {
  return (window.innerWidth / 1920) * font;
}

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: "chart",
    },
    width: {
      type: String,
      default: "100%",
    },
    height: {
      type: String,
      default: "100%",
    },
    barEcahrts: {
      type: Object,
    },
  },
  data() {
    return {
      chart: null,
    };
  },
  watch: {
    barEcahrts: {
      deep: true,
      handler(val) {
        this.setOptions(val);
      },
    },
  },
  mounted() {
    this.$nextTick(() => {
      this.initChart();
    });
  },
  beforeDestroy() {
    if (!this.chart) {
      return;
    }
    this.chart.dispose();
    this.chart = null;
  },
  methods: {
    initChart() {
      this.chart = echarts.init(this.$el);
      this.setOptions(this.barEcahrts);
    },
    setOptions(barEcahrts) {
      const initTooltip = {
        trigger: "item",
        formatter: "{a} <br/>{b} : {c}",
        backgroundColor: "rgba(0,0,0,0.7)",
        borderColor: "transparent",
        textStyle: {
          color: "#fff",
          fontSize: 14,
        },
      };
      const tooltip = barEcahrts.tooltip
        ? { ...initTooltip, ...barEcahrts.tooltip }
        : initTooltip;

      const xAxis = {
        ...{
          type: "category",
          axisTick: {
            show: true,
          },
          axisLine: {
            lineStyle: {
              color: "#333399",
            },
          },
          splitLine: {
            lineStyle: {
              type: "dashed",
            },
          },
          axisLabel: {
            color: "#fff",
            fontSize: 14,
          },
        },
        ...barEcahrts.xAxis,
      };
      let yAxis = null;
      if (Array.isArray(barEcahrts.yAxis)) {
        yAxis = [];
        barEcahrts.yAxis.forEach((ele) => {
          yAxis.push({
            ...{
              type: "value",
              splitNumber: 4,
              name: barEcahrts.unit ? barEcahrts.unit : "",
              axisTick: {
                show: false,
              },
              axisLine: {
                lineStyle: {
                  color: "#333399",
                },
              },
              splitLine: {
                show: false,
                lineStyle: {
                  type: "dashed",
                },
              },
              axisLabel: {
                color: "#fff",
                fontSize: 14,
              },
            },
            ...ele,
          });
        });
      } else {
        yAxis = {
          ...{
            type: "value",
            name: barEcahrts.unit ? barEcahrts.unit : "",
            axisTick: {
              show: false,
            },
            axisLine: {
              lineStyle: {
                color: "#333399",
              },
            },
            splitLine: {
              show: false,
              lineStyle: {
                type: "dashed",
              },
            },
          },
          ...barEcahrts.yAxis,
        };
      }
      this.chart.setOption({
        tooltip: tooltip,
        legend: barEcahrts.legend,
        dataZoom: barEcahrts.dataZoom,
        title: barEcahrts.title ? barEcahrts.title : {},
        grid: {
          ...{
            top: "30%",
            left: "2%",
            right: "2%",
            bottom: "5%",
            containLabel: true,
          },
          ...barEcahrts.grid,
        },
        color: barEcahrts.color ? barEcahrts.color : [],
        xAxis: xAxis,
        yAxis: yAxis,
        series: barEcahrts.series,
      });
    },
  },
};
</script>
